/*
 * Copyright 2010 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.namuan.jp_stock.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
/*
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
*/


import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.pm.Signature;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.CookieSyncManager;
import android.widget.ImageView;
import android.widget.TextView;

public class Justplace {

	// Strings used in the authorization flow

	public static final int FORCE_DIALOG_AUTH = -1;

	private static final String LOGIN = "oauth";

	// Used as default activityCode by authorize(). See authorize() below.;

	// Facebook server endpoints: may be modified in a subclass for testing
	protected static String DIALOG_BASE_URL = "http://m.facebook.com/dialog/";
//	protected static String GRAPH_BASE_URL = "http://192.168.0.3:8080/stocksns/";	
	protected static String GRAPH_BASE_URL = "http://stocksnsserver-34upkdym8c.elasticbeanstalk.com/";
	protected static String RESTSERVER_URL = "http://api.facebook.com/restserver.php";
	
//	public static String PROFILEIMG_URL = "http://192.168.0.3:8080/stocksns/resources/images/user/";
//	public static String ACTIVITYIMG_URL = "http://121.66.86.18:8080/justplace/resources/";
	public static String PROFILEIMG_URL = "http://stocksnsserver-34upkdym8c.elasticbeanstalk.com";
	public static String ACTIVITYIMG_URL = "http://stocksnsserver-34upkdym8c.elasticbeanstalk.com";
	private String mSession = null;
	private String mUserNickname = null;


	private String mProfilepic = null;
	private boolean mAutologin = false;
	private boolean mCheckPopup = false;
	private String tRequstToken=null;
	private String tRequsttokenSecret=null;
	private String tAcessToken=null;
	private Twitter mtwitter;
	
	public Twitter getTwitterApi(){
		if(tRequstToken==null){
			
		return null;	
		}
		mtwitter = new TwitterFactory().getInstance();
		String ConsumerKey ="S7bBCysLO9GBQJMDiXkQ";
		String ConsumerSecret="Xu6KnKdVY1egFy5qRAmA7DS13dn5ZF6Syt0xPFIPh8";
		mtwitter.setOAuthConsumer(ConsumerKey, ConsumerSecret);
		AccessToken acToken = new AccessToken(tRequstToken, tRequsttokenSecret);
		mtwitter.setOAuthAccessToken(acToken);
		return mtwitter;
		
	}
	
	public String gettRequstToken() {
		return tRequstToken;
	}

	public void settRequstToken(String tRequstToken) {
		this.tRequstToken = tRequstToken;
	}

	public String gettRequsttokenSecret() {
		return tRequsttokenSecret;
	}

	public void settRequsttokenSecret(String tRequsttokenSecret) {
		this.tRequsttokenSecret = tRequsttokenSecret;
	}

	public String gettAcessToken() {
		return tAcessToken;
	}

	public void settAcessToken(String tAcessToken) {
		this.tAcessToken = tAcessToken;
	}

	public boolean ismAutologin() {
		return mAutologin;
	}

	public void setmAutologin(boolean mAutologin) {
		this.mAutologin = mAutologin;
	}
	

	public boolean ismCheckPopup() {
		return mCheckPopup;
	}

	public void setmCheckPopup(boolean mCheckPopup) {
		this.mCheckPopup = mCheckPopup;
	}
	
	public String getmSession() {
		return mSession;
	}

	public void setmSession(String mSession) {
		this.mSession = mSession;
	}

	private Activity mAuthActivity;

	public String getmUserNickname() {
		return mUserNickname;
	}

	public void setmUserNickname(String mUserNickname) {
		this.mUserNickname = mUserNickname;
	}
	
	public String getmProfilepic() {
		return mProfilepic;
	}

	public void setmProfilepic(String mProfilepic) {
		this.mProfilepic = mProfilepic;
	}

	public Activity getmAuthActivity() {
		return mAuthActivity;
	}

	public void setmAuthActivity(Activity mAuthActivity) {
		this.mAuthActivity = mAuthActivity;
	}

	private String[] mAuthPermissions;
	private int mAuthActivityCode;

	/**
	 * Constructor for Facebook object.
	 * 
	 * @param userid
	 */
	public Justplace() {

	}

	public String login(String userid, String password)
			throws MalformedURLException, IOException {
		Boolean Loginok = false;
		Bundle b = new Bundle();
		b.putString("usernick", userid);
		b.putString("pw", password);

		String response = request("login.do", b, "GET");

		return response;
	}

	public String logout(Context context) throws MalformedURLException,
			IOException {
		Util.clearCookies(context);
		Bundle b = new Bundle();
		b.putString("method", "auth.expireSession");
		String response = request(b);
		return response;
	}

	public String request(Bundle parameters) throws MalformedURLException,
			IOException {
		if (!parameters.containsKey("method")) {
			throw new IllegalArgumentException("API method must be specified. "
					+ "(parameters must contain key \"method\" and value). See"
					+ " http://developers.facebook.com/docs/reference/rest/");
		}
		return request(null, parameters, "GET");
	}

	public String request(String graphPath) throws MalformedURLException,
			IOException {
		return request(graphPath, new Bundle(), "GET");
	}

	public String request(String graphPath, Bundle parameters)
			throws MalformedURLException, IOException {
		return request(graphPath, parameters, "GET");
	}

	public String request(String graphPath, Bundle params, String httpMethod)
			throws FileNotFoundException, MalformedURLException, IOException {

		String url = (graphPath != null) ? GRAPH_BASE_URL + graphPath
				: RESTSERVER_URL;
		return Util.openUrl(url, httpMethod, params);
	}

	public boolean isSessionValid() {
		return (getmSession() != null);
	}
	public boolean isTwitterSessionValid() {
		return (gettAcessToken() != null);
	}
	public void setHeader(TextView textview) {
		textview.setText(mUserNickname);

	}

}
